﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_DatetimeRangePickerField: PageForm_InputFieldBase
    {
        private string _startName = "";
        private string _endName = "";
        private DateTime? _startValue = null;
        private DateTime? _endValue = null;
        private DateTime? _maxValue = null;
        private DateTime? _minValue = null;
        private string _startPlaceholder = "";
        private string _endPlaceholder = "";
        private string _format = "";
        //private bool _isRequired = false;
        private DatetimePickerType _type = DatetimePickerType.Date;
        private bool _startReadonly = false;
        private bool _endReadonly = false;
        private bool _startDisabled = false;
        private bool _endDisabled = false;
        private bool _startValueRequired = false;
        private bool _endValueRequired = false;

        public PageForm_DatetimeRangePickerField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_datetimerangepickerfield datetimerangepicker");
        }

        public PageForm_DatetimeRangePickerField Require(bool isRequired = true)
        {
            _isRequired = isRequired;

            return this;
        }

        public PageForm_DatetimeRangePickerField Disabled(bool disabled = true)
        {
            _startDisabled = disabled;
            _endDisabled = disabled;

            return this;
        }

        public PageForm_DatetimeRangePickerField Disabled(bool startDisabled,bool endDisabled)
        {
            _startDisabled = startDisabled;
            _endDisabled = endDisabled;
            
            return this;
        }

        public PageForm_DatetimeRangePickerField Format(string format)
        {
            _format = format;
            return this;
        }

        public PageForm_DatetimeRangePickerField Type(DatetimePickerType type)
        {
            _type = type;

            return this;
        }

        public PageForm_DatetimeRangePickerField Name(string preName)
        {
            _startName = preName + "_Start";
            _endName = preName + "_End";

            return this;
        }

        public PageForm_DatetimeRangePickerField Name(string startValueName, string endValueName)
        {
            _startName = startValueName;
            _endName = endValueName;

            return this;
        }

        public PageForm_DatetimeRangePickerField Value(DateTime? startValue=null,DateTime? endValue=null)
        {
            if (startValue.HasValue && endValue.HasValue && startValue>endValue)
            {
                throw new ArgumentOutOfRangeException(nameof(startValue));
            }

            _startValue = startValue;
            _endValue = endValue;

            return this;
        }

        public PageForm_DatetimeRangePickerField ValueLimit(DateTime? minValue=null, DateTime? maxValue=null)
        {
            _maxValue = maxValue;
            _minValue = minValue;

            if (maxValue.HasValue && minValue.HasValue)
            {
                if (maxValue < minValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(maxValue));
                }
            }

            return this;
        }


        public PageForm_DatetimeRangePickerField Placeholder(string startDtPlaceholder="", string endDtPlaceholder="")
        {
            _startPlaceholder = startDtPlaceholder;
            _endPlaceholder = endDtPlaceholder;

            return this;
        }

        public PageForm_DatetimeRangePickerField Required(bool allRequired = true)
        {
            _endValueRequired = allRequired;
            _startValueRequired = allRequired;
            return this;
        }

        public PageForm_DatetimeRangePickerField Required(bool startValueRequired, bool endValueRequired)
        {
            _endValueRequired = endValueRequired;
            _startValueRequired = startValueRequired;

            return this;
        }

        /// <summary>
        /// 所有文本框是否只读
        /// </summary>
        /// <param name="readonly"></param>
        /// <returns></returns>
        public PageForm_DatetimeRangePickerField ReadOnly(bool @readonly = true)
        {
            _startReadonly = @readonly;
            _endReadonly = @readonly;
            return this;
        }

        public PageForm_DatetimeRangePickerField ReadOnly(bool startReadonly,bool endReadonly)
        {
            _startReadonly = startReadonly;
            _endReadonly = endReadonly;
            return this;
        }

        protected override void BeforeRender(TextWriter writer)
        {
            _isRequired = _startValueRequired || _endValueRequired;

            if (_format == "")
            {
                _format = Utility.GetDefaultDateFormat(_type);
            }

            if (_startValue.HasValue && _endValue.HasValue && _startValue.Value>_endValue.Value)
            {
                throw new ArgumentOutOfRangeException("StartDt");
            }

            if (_minValue.HasValue && _startValue.HasValue && _startValue< _minValue)
            {
                _startValue = _minValue;
            }

            if (_maxValue.HasValue && _endValue.HasValue && _maxValue < _endValue)
            {
                _endValue = _maxValue;
            }


            this.Data("pickertype", _type.ToStringEx());
            this.Data("format", _format);
         
            if (_maxValue.HasValue)
            {
                this.Data("maxdt", _maxValue.ToStringEx("yyyy-MM-dd HH:mm:ss"));
            }

            if (_minValue.HasValue)
            {
                this.Data("mindt", _minValue.ToStringEx("yyyy-MM-dd HH:mm:ss"));
            }

            this.Data("data-startdisabled", _startDisabled.ToString());
            this.Data("data-enddisabled",_endDisabled.ToString());

            base.BeforeRender(writer);
        }

        protected override void RenderContent(TextWriter writer)
        {

            //ViewContext.Writer.WriteLine("<div class=\"input-group\">");

            ViewContext.Writer.WriteLine($"<input type=\"text\" class=\"form-control startvalue\" {(_isRequired ? "required" : "")} {_startReadonly.If("readonly")} name=\"{_startName}\" value=\"{_startValue.ToStringEx(_format)}\" placeholder=\"{_startPlaceholder}\">");
            ViewContext.Writer.WriteLine("<span class=\"input-group-addon\">-</span>");
            ViewContext.Writer.WriteLine($"<input type=\"text\" class=\"form-control endvalue\" {(_isRequired ? "required" : "")} {_endReadonly.If("readonly")} name=\"{_endName}\" value=\"{_endValue.ToStringEx(_format)}\" placeholder=\"{_endPlaceholder}\">");

            //ViewContext.Writer.WriteLine("</div> <!-- .input-group -->");
        }

        protected override string ErrorMessage
        {
            get
            {
                return GetErrorMessageByNames(_startName, _endName);

                //var errorMsg = "";

                //if (ViewContext.ModelState.TryGetValue(_startName, out var error)) // ModelMetadata.FromStringExpression(_name, ViewContext.ViewData);
                //{
                //    errorMsg = error.Errors.Select(x => x.ErrorMessage).JoinToString('|');
                //}

                //if (ViewContext.ModelState.TryGetValue(_endName, out var error1)) // ModelMetadata.FromStringExpression(_name, ViewContext.ViewData);
                //{
                //    errorMsg = errorMsg + error1.Errors.Select(x => x.ErrorMessage).JoinToString('|');
                //}

                //return errorMsg;
            }
        }

        protected override bool HasError
        {
            get
            {
                return IsHasErrorByNames(_startName, _endName);

                //var hasError = false;

                //if (ViewContext.ViewData.ModelState.TryGetValue(_startName, out var errors))
                //{
                //    hasError = errors.Errors.HasData();
                //}

                //if (hasError && ViewContext.ViewData.ModelState.TryGetValue(_endName, out var errors1))
                //{
                //    hasError = errors1.Errors.HasData();
                //}

                //return hasError;
            }
        }
    }
}
